package com.woniuxy.mapper;

import com.woniuxy.model.entity.TransactionOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【transaction_order】的数据库操作Mapper
* @createDate 2025-01-06 10:10:10
* @Entity com.woniuxy.model.entity.TransactionOrder
*/
@Mapper
public interface TransactionOrderMapper extends BaseMapper<TransactionOrder> {

    /**
     * 根据订单号查询订单
     */
    @Select("SELECT * FROM transaction_order WHERE order_no = #{orderNo}")
    TransactionOrder selectByOrderNo(@Param("orderNo") String orderNo);

    @Select("SELECT " +
            "t.order_no AS orderNumber, " +  // 将 to 改为 t
            "cp.pile_code AS pileCode, " +
            "s.station_name AS stationName, " +
            "t.transaction_amount AS transactionAmount, " +  // 将 to 改为 t
            "CASE " +
            "    WHEN t.status = 0 THEN '充电中' " +  // 将 to 改为 t
            "    WHEN t.status = 1 THEN '已结束' " +  // 将 to 改为 t
            "    ELSE '未知状态' " +
            "END AS chargingStatus " +
            "FROM " +
            "transaction_order t " +  // 将 to 改为 t
            "LEFT JOIN " +
            "charging_pile cp ON t.pile_id = cp.id " +  // 将 to 改为 t
            "LEFT JOIN " +
            "station s ON t.station_id = s.id " +  // 将 to 改为 t
            "WHERE " +
            "t.customer_id = #{customerId} " +  // 将 to 改为 t
            "AND t.payment_status = 0")  // 将 to 改为 t
    List<Map<String, Object>> getPendingPaymentOrders(@Param("customerId") Long customerId);

    @Select("SELECT " +
            "t.order_no AS orderNumber, " +  // 将 to 改为 t
            "cp.pile_code AS pileCode, " +
            "s.station_name AS stationName, " +
            "t.transaction_amount AS transactionAmount, " +  // 将 to 改为 t
            "CASE " +
            "    WHEN t.status = 0 THEN '充电中' " +  // 将 to 改为 t
            "    WHEN t.status = 1 THEN '已结束' " +  // 将 to 改为 t
            "    ELSE '未知状态' " +
            "END AS chargingStatus " +
            "FROM " +
            "transaction_order t " +  // 将 to 改为 t
            "LEFT JOIN " +
            "charging_pile cp ON t.pile_id = cp.id " +  // 将 to 改为 t
            "LEFT JOIN " +
            "station s ON t.station_id = s.id " +  // 将 to 改为 t
            "WHERE " +
            "t.customer_id = #{customerId} " +  // 将 to 改为 t
            "AND t.payment_status = 1")  // 将 to 改为 t
    List<Map<String, Object>> getCompletedOrders(@Param("customerId") Long customerId);

    @Select("SELECT " +
            "t.order_no AS orderNumber, " +  // 将 to 改为 t
            "t.transaction_amount AS transactionAmount, " +  // 将 to 改为 t
            "t.status AS orderStatus, " +  // 将 to 改为 t
            "t.payment_status AS paymentStatus, " +  // 将 to 改为 t
            "t.create_time AS createTime, " +  // 将 to 改为 t
            "t.update_time AS updateTime, " +  // 将 to 改为 t
            "t.remark AS remark, " +  // 将 to 改为 t
            "cp.pile_code AS pileCode, " +
            "s.station_name AS stationName, " +
            "CASE " +
            "    WHEN t.status = 0 THEN '充电中' " +  // 将 to 改为 t
            "    WHEN t.status = 1 THEN '已结束' " +  // 将 to 改为 t
            "    ELSE '未知状态' " +
            "END AS chargingStatus, " +
            "CASE " +
            "    WHEN t.payment_status = 0 THEN '待付款' " +  // 将 to 改为 t
            "    WHEN t.payment_status = 1 THEN '已付款' " +  // 将 to 改为 t
            "    ELSE '未知状态' " +
            "END AS paymentStatus " +
            "FROM " +
            "transaction_order t " +  // 将 to 改为 t
            "LEFT JOIN " +
            "charging_pile cp ON t.pile_id = cp.id " +  // 将 to 改为 t
            "LEFT JOIN " +
            "station s ON t.station_id = s.id " +  // 将 to 改为 t
            "WHERE " +
            "t.order_no = #{orderNumber}")  // 将 to 改为 t
    Map<String, Object> getOrderDetails(@Param("orderNumber") String orderNumber);



    int updateRefundStatus(@Param("orderNumber") String orderNumber, @Param("status") int status);
}
